package com.qiantu.phone.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SoundEffectConstants;
import android.view.View;
import android.view.ViewConfiguration;

import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;

public class SwitchDeviceView extends View {
    private static final int CLICK_TIME = 100; //快速点击间隔时间
    private int mScaledTouchSlop;
    // 画笔
    private Paint mPaint;
    private long lastClickTime;
    private Drawable centerDrawable;
    private int strokeWidth;
    private int shadow;
    private LinearGradient mGradient;
//    private Matrix gradientMatrix;
    private boolean isOpen;
    private OnClickListener mOnClickListener;
    private int deviceSolidBgColor;

    public SwitchDeviceView(Context context) {
        this(context, null);
    }

    public SwitchDeviceView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SwitchDeviceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = attrs == null ? null : getContext().obtainStyledAttributes(attrs, R.styleable.SwitchDeviceView);
        if (ta != null) {
            centerDrawable = ta.getDrawable(R.styleable.SwitchDeviceView_center_drawable);
            ta.recycle();
        }
        mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        strokeWidth = getResources().getDimensionPixelSize(R.dimen.dp_23);
        shadow = getResources().getDimensionPixelSize(R.dimen.dp_5);
        deviceSolidBgColor = AppApplication.getInstance().getThemeColor(R.attr.deviceSolidBgColor);
//        gradientMatrix = new Matrix();
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
//                long time = System.currentTimeMillis();
//                if (time - lastClickTime < CLICK_TIME) {//判断系统时间差是否小于点击间隔时间
//                    return true;
//                }
//                lastClickTime = time;
                float x = event.getX();
                float y = event.getY();
                float distance = (float) Math.sqrt(Math.pow(Math.abs(getWidth() / 2 - x), 2) + Math.pow(Math.abs(getHeight() / 2 - y), 2));
                if(distance <= getWidth() / 2){
                    if(mOnClickListener != null){
                        playSoundEffect(SoundEffectConstants.CLICK);
                        mOnClickListener.onClick();
                    }
                }
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
        }
        return true;
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        int x = getWidth() / 2;
        int y = getHeight() / 2;
//        gradientMatrix.setTranslate(x,y);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(deviceSolidBgColor);
//        mPaint.setShadowLayer(shadow, 1, 1, 0x40979797);
        canvas.drawCircle(x, y, getWidth() / 2 - strokeWidth, mPaint);
        mPaint.setMaskFilter(new BlurMaskFilter(shadow, BlurMaskFilter.Blur.NORMAL));
        mPaint.setColor(0x33979797);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(shadow);
        canvas.drawCircle(x, y, (getWidth() / 2 - strokeWidth), mPaint);
        mPaint.setColor(0xff979797);
        mPaint.setStrokeWidth(getResources().getDimensionPixelSize(R.dimen.dp_1));
        canvas.drawCircle(x, y, (getWidth() / 2 - strokeWidth), mPaint);
        mPaint.setMaskFilter(null);

        if(isOpen){
            if(mGradient == null){
                mGradient = new LinearGradient(0, 0, getWidth(), getHeight(), 0xffFFDA7D, 0xffFFB347, Shader.TileMode.CLAMP);
            }
            mPaint.setShader(mGradient);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(strokeWidth);
            canvas.drawCircle(x, y, getWidth() / 2 - mPaint.getStrokeWidth() / 2, mPaint);
            mPaint.setShader(null);
        }else{
            mPaint.setColor(AppApplication.getInstance().getThemeColor(R.attr.secondaryColor10));
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(strokeWidth);
            mPaint.setShadowLayer(0, 0, 0, 0);
            canvas.drawCircle(x, y, getWidth() / 2 - strokeWidth / 2, mPaint);
        }

        if(centerDrawable != null){
            centerDrawable.setBounds(x - centerDrawable.getIntrinsicWidth() / 2,
                    y - centerDrawable.getIntrinsicHeight() / 2,
                    x + centerDrawable.getIntrinsicWidth() / 2,
                    y + centerDrawable.getIntrinsicHeight() / 2);
            centerDrawable.draw(canvas);
        }
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void setState(boolean isOpen){
        this.isOpen = isOpen;
        invalidate();
    }

    public boolean isOpen() {
        return isOpen;
    }

    public interface OnClickListener {
        void onClick();
    }

    public void setOnClickListener(OnClickListener listener) {
        this.mOnClickListener = listener;
    }

    /**
     * dp转px
     *
     * @param dp dp值
     * @return px值
     */
    public int dp2px(float dp) {
        return (int) (getResources().getDimensionPixelSize(R.dimen.dp_1) * dp);
    }
}
